﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Exhibition.Common.CacheManager;
using Exhibition.Common.Helper;
using Exhibition.Dao.ViewModel;
using Exhibition.Service;

namespace Exhibition
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "用户注册--"+WebTitle;

            var user = StringUtils.GetCookie("EXUser", "UserName");
            if (!string.IsNullOrEmpty(user))
            {
                chk.Checked = true;
                login_name.Value = user;
            }

        }

        protected void btnReg_Click(object sender, EventArgs e)
        {
            Response.Redirect("Reg.aspx");
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            var uname = login_name.Value;
            var pwd = string.IsNullOrEmpty(txtPwd.Value.Trim()) ? "" : StringUtils.EncryptPassWord(txtPwd.Value.Trim(), StringUtils.PassFormat.MD5);
            if (string.IsNullOrEmpty(uname))
            {
                JsHelper.Alert(this, CommFunMsg.GetMessage("A0003", Lan.zh));
                return;
            }
            if (string.IsNullOrEmpty(pwd))
            {
                JsHelper.Alert(this, CommFunMsg.GetMessage("A0004", Lan.zh));
                return;
            }
            UserModel umd = CenterService.userService.UserLogin(uname, pwd);
            if (umd != null)
            {
                Session["LoginUser"] = umd;
                if (chk.Checked)
                {

                    StringUtils.SetCookie("EXUser", "UserName", uname);
                }
                //用户名放入cookie
                FormsAuthentication.SetAuthCookie(uname, false);
                //用户信息放入缓存
                CommonCacheManager.AddCacheData<UserModel>("CurrentUser" + uname, umd, 60);
                //FormsAuthentication.GetAuthCookie()
                Response.Redirect("UserCenter/Index.aspx");


            }
            else
            {
                JsHelper.Alert(this, CommFunMsg.GetMessage("A0005", Lan.zh));
                return;
            }


        }
    }
}